package org.ben.gather;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @Author: Ben
 * @Date: 2019/10/16 19:20
 */
public class SetTest {
    public static void main(String[] args) {
        Set<String> stringSet=new HashSet<>();
        stringSet.add("Java");
        stringSet.add("Pyhton");
        stringSet.add("C");
        stringSet.add("C++");
        stringSet.add("Matlab");
        stringSet.add("R");
        stringSet.add("html");
        stringSet.add("CSS");
        stringSet.add("JavaScript");
        SetTest setTest=new SetTest();
        setTest.forEachStringSet(stringSet);
        Set<Integer> integerSet=new HashSet<>();
        for (int i = 0; i <10 ; i++) {
            integerSet.add(i);
        }
        setTest.forEachIntegerSet(integerSet);
        System.out.println("---------------------------------------");
        integerSet.remove(0);
        setTest.IteratorIntegerSet(integerSet);
        Set<User> userSet=new HashSet<>();
        userSet.add(new User(1,"mjz","mjz","mjz","mjz"));
        userSet.add(new User(2,"aaa","aaa","aaa","aaa"));
        userSet.add(new User(3,"bbb","bbb","bbb","bbb"));
        userSet.add(new User(4,"ccc","ccc","ccc","ccc"));
        userSet.add(new User(5,"ddd","ddd","ddd","ddd"));
        userSet.add(new User(6,"eee","eee","eee","eee"));
        userSet.add(new User(7,"fff" ,"fff","fff","fff"));
        setTest.IteratorUserSet(userSet);
    }

    /**
     * 用foreach循环迭代Set<String>集合
     * @param set
     */
    public void forEachStringSet(Set<String> set){
        for (String a:set) {
            System.out.println(a);
        }
    }

    /**
     * 用foreach循环迭代Set<Integer>集合
     * @param set
     */
    public void forEachIntegerSet(Set<Integer> set){
        for (Integer a:set) {
            System.out.println(a);
        }
    }

    /**
     * 用Iterator迭代器迭代Set<Integer>集合
     * @param set
     */
    public void IteratorIntegerSet(Set<Integer> set){
        Iterator<Integer> iterator=set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    /**
     * 用Iterator迭代器迭代Set<User>集合
     * @param set
     */
    public void IteratorUserSet(Set<User> set){
        Iterator<User> iterator=set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
    }
}